package org.lmind.mis.webapp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.core.Configurable;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * Application Lifecycle Listener implementation class ApplicationLoader
 *
 */

public class ApplicationLoader implements ServletContextListener {
	
	private static Logger LOG = LoggerFactory.getLogger(ApplicationLoader.class);

	/**
     * Default constructor. 
     */
    public ApplicationLoader() {
    }

	public void contextDestroyed(ServletContextEvent arg0) {
		
	}


	public void contextInitialized(ServletContextEvent sce) {
		
		LOG.debug("init");
//		try {
//			DOMConfigurator.configure(servletContext.getResource("/WEB-INF/log4j.xml"));
//			LOG.info("日志初始化成功");
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		}
		
//		try {
//			UrlResource res = new UrlResource(servletContext.getResource("/WEB-INF/classes/applicationContext.xml"));
//			appContext = new GenericXmlApplicationContext(res);
//			LOG.info("applicationContext.xml");
//			
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		}

	}
	
	public static void main(String[] argv) {
		
		try {
			
			Configuration cfg = new Configuration();
			cfg.setDirectoryForTemplateLoading(new File("F:/workspace/org.lmind.mis.webapp/"));
			
			Template template = cfg.getTemplate("src/main/webapp/ftl/hello.ftl");
			
			HashMap<String, String> root = new HashMap<String, String>();
			root.put("message", "123");
			template.process(root, new OutputStreamWriter(new FileOutputStream("D:\\t.html")));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
